package com.woniuxy01;

class Aaa{
}
class Bbb extends Aaa{
}
class Ccc extends Aaa{
}

public class Test01 {
    public static void main(String[] args) {
        Aaa aaa=new Aaa();
        Bbb bbb=new Bbb();

        aaa=bbb;

        System.out.println(aaa instanceof  Aaa);
        System.out.println(aaa instanceof  Bbb);
        if (aaa instanceof  Bbb) {
            System.out.println("兼容类型，执行转换");
            bbb= (Bbb) aaa;
        }
        System.out.println(bbb instanceof  Aaa);
        System.out.println(bbb instanceof  Bbb);
    }

    private static void test04() {
        Aaa aaa=new Aaa();
        Bbb bbb=new Bbb();
        //向下转型，不安全
        //假转型:ok
        aaa=bbb;
        bbb= (Bbb) aaa;
    }

    private static void test03() {
        Aaa aaa=new Aaa();
        Bbb bbb=new Bbb();
        //向下转型，不安全
        //真转型:编译报错，使用强转类型转换，编译OK；运行时还是异常
        bbb= (Bbb) aaa;
    }

    private static void test02() {
        Bbb bbb=new Bbb();
        Ccc ccc=new Ccc();
        //兄弟类之间都不能转换
//        ccc=(Ccc)bbb;
    }


    private static void test01() {
        Aaa aaa=new Aaa();
        Bbb bbb=new Bbb();
        //向上转型:子类对象赋值给父类，安全
        aaa=bbb;
    }
}
